//
//  WQSelectOrderVC.m
//  货运代码
//
//  Created by 刘胤辰 on 16/5/5.
//  Copyright © 2016年 温泉. All rights reserved.
//

#import "FDAttentionController.h"
#import <Masonry.h>
#import "FDAttentionCell.h"
#import "FDMDataModel.h"
#import "FDDocterInfoController.h"
#import "AppDelegate.h"

@interface FDAttentionController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic , weak)UITableView *attentionView;
@property (nonatomic , strong)NSArray <FDMDataModel*>*dataArray;
@end

@implementation FDAttentionController

/**数据源数组一发生改变就刷新界面*/
- (void)setDataArray:(NSArray<FDMDataModel *> *)dataArray
{
    _dataArray = dataArray;
    [self.attentionView reloadData];
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:YES];
    
    //监测网络
    AppDelegate *appDelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
    
    if (!appDelegate.isAchieve) {
        
        [self presentAlertVC:@"⚠️网络异常，请检查是否连接网络"];
    }
}

-(void)presentAlertVC:(NSString *)str{
    
    //创建网络异常弹框
    UIAlertController *alertVC=[UIAlertController alertControllerWithTitle:str message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [self presentViewController:alertVC animated:YES completion:nil];
    
    UIAlertAction *identify=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    UIAlertAction *cancle=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [alertVC addAction:cancle];
    [alertVC addAction:identify];
}

-(void)viewDidLoad{
    
    [super viewDidLoad];

    //创建tableView
    [self setupTableView];
    
    //网络获取数据
    [FDMDataModel dataArrayWithSuccessBlock:^(NSArray<FDMDataModel *> *dataArray) {
        
        self.dataArray = dataArray;
   
    } errorBlock:^{
        NSLog(@"error");
    }];
}

/**创建tableView*/
- (void)setupTableView
{
    UITableView *attentionView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.attentionView = attentionView;
    [self.view addSubview:attentionView];
    attentionView.dataSource = self;
    attentionView.delegate = self;
    attentionView.rowHeight = SCREEN_WIDTH*0.32;
    attentionView.backgroundColor = [UIColor colorWithRed:247/255.0 green:247/255.0 blue:247/255.0 alpha:1];
    //取消cell的线条
    attentionView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //设置约束
    [attentionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.offset(0);
    }];
}

/**数据源代理方法*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *const ID = @"attention";
    FDAttentionCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[FDAttentionCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    FDMDataModel *dataModel = self.dataArray[indexPath.row];
    cell.dataModel = dataModel;

    return cell;
}

/**点击cell*/
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    FDDocterInfoController * VC = [[FDDocterInfoController alloc] init];
    FDMDataModel *model = self.dataArray[indexPath.row];
    VC.dataModel = model;
    [self.navigationController pushViewController:VC animated:YES];
}

@end
